home *** CD-ROM | disk | FTP | other *** search
- /* Screen editor: general utilities
- *
- * Module: ed9/ccc
- * Date: November 12, 1983
- * Changed: February 17, 1984
- * Changed: February 29, 1984 per VMEDFX.002
- * Changed: March 10, 1984 - accept last line w/o CR
- * Changed: July 5, 1984 - correct bug in 3-10 fix
- */
-
- #include ed0
-
- /* Data global to this module */
-
- int recalln; /* remembered line number for goto etc. */
-
- /* return: is first token in args a number ?
- * return value of number in *val
- *
- * special cases made for 't'op, 'b'ottom and '.' (current line)
- * and 'r'emembered line
- */
- number(args,val) char *args; int *val;
- { char c;
- unsigned num;
- switch (c = tolower(*args++)) {
- case '.':
- num = bufln(); break;
- case 't':
- num = 1; break;
- case 'b':
- case '$':
- num = buffree(); break;
- case 'r':
- num = rememln(0); break;
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- num = c - '0';
- while (isdigit(c = *args++))
- num = (num*10) + (c - '0');
- break;
- default:
- return(NO);
- }
- *val = max(1, num); /* 7-5-84 */
- return(YES);
- }
-
-
- /* stores or returns remembered line */
-
- rememln(line) int line;
- { if (line == 0)
- return(recalln);
- else
- recalln = line;
- }
-
- max(m,n) int m,n; /* return maximum of m,n */
- { return ((m > n) ? m : n); }
-
- min(m,n) int m,n; /* return minimum of m,n */
- { return ((m > n) ? n : m); }
-
- /* put decimal integer n in field width >= w.
- * left justify the number in the field.
- */
- putdec(n,w) unsigned int n,w; /* 03/03/84 jwk */
- { char chars[10];
- int i,nd;
- nd = itoc(n,chars,10);
- i = 0;
- while (i < nd)
- syscout(chars[i++]);
- i = nd;
- while (i++ < w)
- syscout(' ');
- }
-
- /* convert integer n to character string in str */
- itoc(n,str,size) int n,size; char *str;
- { int absval,len,i,j,k;
- absval = abs(n);
- /* generate digits */
- str[0] = 0;
- i = 1;
- while (i < size) {
- str[i++] = (absval%10) + '0';
- absval = absval/10;
- if (absval == 0)
- break;
- }
- /* generate sign */
- if ((i < size) & (n < 0))
- str[i++] = '-';
- len = i - 1;
- /* reverse sign, digits */
- --i;
- j = 0;
- while (j < i) {
- k = str[i];
- str[i] = str[j];
- str[j] = k;
- --i;
- ++j;
- }
- return(len);
- }
-
- /* return absolute value of n */
- abs(n) int n;
- { return ((n < 0) ? -n : n); }
-
- /* system error routine */
- syserr(s) char *s;
- { pmtmess("system error: ",s); }
-
- /* user error routine */
- error(s) char *s;
- { pmtmess("error: ",s); }
-
- /* read the next line of the file into
- * the buffer of size n that p points to.
- * Successive calls to readline() read the file
- * from front to back.
- */
- readline(file,p,n) int file,n; char *p;
- { int c,k;
- k = 0;
- FOREVER {
- switch (c = sysrdch(file)) {
- case EOF:
- return (k > 0 ? k : EOF); /* 7-5-84 */
- case ERR:
- return(ERR);
- case CR:
- return(k);
- default:
- break;
- }
- if (k < n)
- *p++ = c; /* move character to buffer */
- ++k; /* always bump count */
- }
- }
-
- /* push (same as write) line to end of file.
- * line is in the buffer of size n that p points to.
- */
- pushline(file,p,n) int file,n; char *p;
- {
- /* write all but trailing CR */
- while ((n--) > 0)
- if (syspshch(*p++,file) == ERR)
- return(ERR);
- /* write trailing CR */
- return(syspshch(CR,file));
- }
-
- /* end module ed9/ccc */
-